/* Title:		
 *
 * Project:     
 *
 * @ Link: 		http://...
 *
 * @ Email:		ivan_ling@hotmail.com
 *
 * @ Copyright: Copyright (c) 2008 mezimedia
 *
 * @ Author 	Ivan.ling
 *
 * @ Version 	1.0
 
 * @ last change time 2008-01-20 	
 */
package com.pub.util;

import java.util.ArrayList;
import java.util.HashMap;

public class TableUtils
{
	String strDefaultContent;
	String strTableAttribute = "";
	String strTdAttribute	 = "";
	String[][] 	arrContent;
	ArrayList<ArrayList<String>> alContent;
	
	int intRowLength, intColumnLength;
		
	HashMap<Integer, HashMap<Integer, String>> hmtTdRowAttr = new HashMap<Integer, HashMap<Integer, String>>();
	HashMap<Integer, String> hmIntStrTemplates 	= new HashMap<Integer, String>();
	HashMap<Integer, String> hmTrAttr 			= new HashMap<Integer, String>();

	public static void main(String args[])
	{
		TableUtils t = new TableUtils("");
		t.addAutoContent("String strFirstColumn", "String strSecondColumn");
		t.addAutoContent("String 111111", "String strS222222econdColumn");
		System.out.println("html === "+t.outAutoRowHTML());
	}
	
	/**
	 * 
	 * @param intRowNum
	 * @param intColumnNum
	 */
	public TableUtils(int intRowNum, int intColumnNum)
	{
		intRowLength 	= intRowNum;
		intColumnLength = intColumnNum;
		
		arrContent 		= new String[intRowLength][intColumnLength];
		strDefaultContent = "&nbsp;";
	}
	
	public TableUtils(int intColumnNum)
	{
		this.intColumnLength = intColumnNum;
		
		this.alContent = new ArrayList<ArrayList<String>>();
		strDefaultContent = "&nbsp;";
	}
	
	public TableUtils(String strBgcolor)
	{
		this.intColumnLength = 2;
		
		setTableAttribute(" width='100%' border='0' align='center' cellpadding='5' cellspacing='1' bordercolor='#333333' bgcolor='#" + strBgcolor + "'");
		setAllTdAttribute(" height=28 bgcolor='#FFFFFF'");
		addTdAttribute(0, 0, " width='20%'");

		this.alContent = new ArrayList<ArrayList<String>>();
		strDefaultContent = "&nbsp;";
	}
	
	public TableUtils(String strBgcolor, String strBdcolor, int iColumnLimit)
	{
		this.intColumnLength = iColumnLimit;
		
		setTableAttribute(" width='100%' border='1' align='center' cellpadding='5' cellspacing='0' bordercolor='#" + strBdcolor + "' bgcolor='#" + strBgcolor + "'");
		
		setAllTdAttribute(" height=28 bgcolor='#FFFFFF'");
		addTdAttribute(0, 0, " width='20%'");

		this.alContent = new ArrayList<ArrayList<String>>();
		strDefaultContent = "&nbsp;";
	}
	
	/* create table html source code
	 * @ param 	intRowNum 		row amount
	 * @ param 	intColumnNum 	column amount
	 * @ param 	alContent 		table content
	 * @ return table html code
	 */
	public static String createTableHTML(int intRowNum,int intColumnNum, ArrayList<String> alContent, String strDefaultContent)
	{
		StringBuilder sbHTMLcache = new StringBuilder(250);
		sbHTMLcache.append("<table>");
		int c = 0;
		int intContentLength = alContent.size();
		for(int i = 0; i < intRowNum; i++){
			sbHTMLcache.append("<tr>");
			for(int j = 0; j < intColumnNum; j++){
				sbHTMLcache.append("<td>");
				if(c < intContentLength)
					sbHTMLcache.append(alContent.get(c));
				else
					sbHTMLcache.append(strDefaultContent);

				sbHTMLcache.append("</td>");
				c++;
			}
			sbHTMLcache.append("</tr>");
		}
		sbHTMLcache.append("</table>");
		return 	sbHTMLcache.toString();
	}

	/* create table html source code
	 * @ param 	alContent 			table content
	 * @ param 	intColumnNum 		column amount ,auto row amount
	 * @ param 	strDefaultContent	default table content
	 * @ return table html code
	 */
	public static String createTableHTML(int intColumnNum, ArrayList<String> alContent, String strDefaultContent)
	{
		StringBuilder sbHTMLcache = new StringBuilder(250);
		sbHTMLcache.append("<table>");
		int c = 0;
		int intContentLength = alContent.size();
		int intRowNum = (int)(Math.floor(intContentLength / intColumnNum + intContentLength % intColumnNum));
		
		for(int i = 0; i < intRowNum; i++){
			sbHTMLcache.append("<tr>");
			for(int j = 0; j < intColumnNum; j++){
				sbHTMLcache.append("<td>");
				if(c < intContentLength)
					sbHTMLcache.append(alContent.get(c));
				else
					sbHTMLcache.append(strDefaultContent);

				sbHTMLcache.append("</td>");
				c++;
			}
			sbHTMLcache.append("</tr>");
		}
		sbHTMLcache.append("</table>");
		return 	sbHTMLcache.toString();
	}

	/* create table html source code
	 * @ param 	intColumnNum 	column amount
	 * @ param 	alContent 		table content
	 * @ param 	strTableAttr 	table tag attribute
	 * @ param 	strTrAttr 		table row attribute
	 * @ param 	strTdAttr 		table column attribute
	 * @ return table html code
	 */
	public static String createTableHTML(int intColumnNum, ArrayList<String> alContent, String strDefaultContent, String strTableAttr, String strTrAttr, String strTdAttr, HashMap<Integer, HashMap<Integer, String>> hmtTrAttrRow)
	{
		StringBuilder sbHTMLcache = new StringBuilder(250);
		sbHTMLcache.append("<table");
		sbHTMLcache.append(strTableAttr);
		sbHTMLcache.append(">");
		int c = 0;
		int intContentLength = alContent.size();
		int intRowNum = (int)(Math.floor(intContentLength/intColumnNum + intContentLength%intColumnNum));
		
		for(int i = 0; i < intRowNum; i++){
			sbHTMLcache.append("<tr");
			sbHTMLcache.append(strTrAttr);
			sbHTMLcache.append(">");
			for(int j = 0; j < intColumnNum; j++){
				sbHTMLcache.append("<td");
				sbHTMLcache.append(strTdAttr);
				if(hmtTrAttrRow != null && hmtTrAttrRow.get(i)!= null && hmtTrAttrRow.get(i).get(j) != null)
					sbHTMLcache.append(hmtTrAttrRow.get(i).get(j));
				sbHTMLcache.append(">");
				if(c < intContentLength)
					sbHTMLcache.append(alContent.get(c));
				else
					sbHTMLcache.append(strDefaultContent);
					
				sbHTMLcache.append("</td>");
				c++;
			}
			sbHTMLcache.append("</tr>");
		}
		sbHTMLcache.append("</table>");
		return 	sbHTMLcache.toString();
	}


	/* create table html source code
	 * @ param 	intRowNum 		row amount
	 * @ param 	intColumnNum 	column amount
	 * @ param 	alContent 		table content
	 * @ param 	strTableAttr 	table tag attribute
	 * @ param 	strTrAttr 		table row attribute
	 * @ param 	strTdAttr 		table column attribute
	 * @ return table html code
	 */
	public static String createTableHTML(int intRowNum,int intColumnNum, ArrayList<String> alContent, String strDefaultContent, String strTableAttr, String strTrAttr, String strTdAttr)
	{
		StringBuilder sbHTMLcache = new StringBuilder(250);
		sbHTMLcache.append("<table");
		sbHTMLcache.append(strTableAttr);
		sbHTMLcache.append(">");
		int c = 0;
		int intContentLength = alContent.size();
		for(int i = 0; i < intRowNum; i++){
			sbHTMLcache.append("<tr");
			sbHTMLcache.append(strTrAttr);
			sbHTMLcache.append(">");
			for(int j = 0; j < intColumnNum; j++){
				sbHTMLcache.append("<td");
				sbHTMLcache.append(strTdAttr);
				sbHTMLcache.append(">");
				if(c < intContentLength)
					sbHTMLcache.append(alContent.get(c));
				else
					sbHTMLcache.append(strDefaultContent);
					
				sbHTMLcache.append("</td>");
				c++;
			}
			sbHTMLcache.append("</tr>");
		}
		sbHTMLcache.append("</table>");
		return 	sbHTMLcache.toString();
	}

	/* create table html source code
	 * @ param 	intRowNum 		row position
	 * @ param 	intColumnNum 	column position
	 * @ param 	strContent 		table content
	 * @ return
	 */
	public void addContent(int intRowNum, int intColumnNum, String strContent)
	{
		if(intRowNum < intRowLength && intColumnNum < intColumnLength && strContent != null)
			arrContent[intRowNum][intColumnNum] = strContent;
	}

	/* add auto row content
	 * @ param 	intRowNum 		row position
	 * @ param 	intColumnNum 	column position
	 * @ param 	strContent 		table content
	 * @ return
	 */
	public boolean isAddAutoContent(ArrayList<String> alRowContent)
	{
		if(alRowContent == null || alRowContent.size() != this.intColumnLength)
			return false;
		
		this.alContent.add(alRowContent);
		
		return true;
	}
	
	/* add auto row content
	 * @ param 	intRowNum 		row position
	 * @ param 	intColumnNum 	column position
	 * @ param 	strContent 		table content
	 * @ return
	 */
	public boolean addAutoContent(String strFirstColumn, String strSecondColumn)
	{
		ArrayList<String> alCache = new ArrayList<String>(this.intColumnLength);
		alCache.add(strFirstColumn);
		alCache.add(strSecondColumn);
		
		this.alContent.add(alCache);
		
		return true;
	}
	
	/* add table row attribute
	 * @ param 	intRowNum 		row position
	 * @ param 	strContent 		attribute content
	 * @ return
	 */
	public void addTrAttribute(int intRowNum, String strContent)
	{
		hmTrAttr.put(intRowNum, strContent);
	}

	/* add table column attribute
	 * @ param 	intRowNum 		row position
	 * @ param 	intColumnNum 	column position
	 * @ param 	strContent 		attribute content
	 * @ return
	 */
	@SuppressWarnings("unchecked")
	public void addTdAttribute(int intRowNum, int intColumnNum, String strContent)
	{
		HashMap<Integer, String> hmTdColumnAttr = hmtTdRowAttr.get(intRowNum);
		if(hmTdColumnAttr == null)
			hmTdColumnAttr = (HashMap<Integer, String>)hmIntStrTemplates.clone();
			
		hmTdColumnAttr.put(intColumnNum, strContent);
		hmtTdRowAttr.put(intRowNum, hmTdColumnAttr);

	}

	/* add table column attribute
	 * @ param 	intRowNum 		row position
	 * @ param 	intColumnNum 	column position
	 * @ param 	strContent 		attribute content
	 * @ return
	 */
	public void setAllTdAttribute(String strContent)
	{
		strTdAttribute = strContent;
	}

	/* set table attribute
	 * @ param 	strAttribute	table attribute
	 * @ return
	 */
	public void setTableAttribute(String strAttribute)
	{
		strTableAttribute = strAttribute;
	}

	/* set table default content
	 * @ param 	strContent	table content
	 * @ return
	 */
	public void setDefaultContent(String strContent)
	{
		strDefaultContent = strContent;
	}
	
	/* out table html code
	 * @ return table html code
	 */
	public String outHTML()
	{
		StringBuilder sbHTMLcache = new StringBuilder(250);
		sbHTMLcache.append("<table");
		sbHTMLcache.append(strTableAttribute);
		sbHTMLcache.append(">");
		
		for(int i = 0; i < intRowLength; i++){
			sbHTMLcache.append("<tr");
			if(hmTrAttr.get(i)!= null)
				sbHTMLcache.append(hmTrAttr.get(i));
			sbHTMLcache.append(">");

			for(int j = 0; j < intColumnLength; j++){
				sbHTMLcache.append("<td");
				sbHTMLcache.append(strTdAttribute);
				
				if(hmtTdRowAttr.get(i)!= null && hmtTdRowAttr.get(i).get(j) != null)
					sbHTMLcache.append(hmtTdRowAttr.get(i).get(j));
				sbHTMLcache.append(">");
				if(arrContent[i][j] != null)
					sbHTMLcache.append(arrContent[i][j]);
				else
					sbHTMLcache.append(strDefaultContent);
				sbHTMLcache.append("</td>");
			}
			sbHTMLcache.append("</tr>");
		}
		sbHTMLcache.append("</table>");
		
		return sbHTMLcache.toString();
	}
	
	/* out table html code
	 * @ return table html code
	 */
	public String outAutoRowHTML()
	{
		StringBuilder sbHTMLcache = new StringBuilder(500);
		sbHTMLcache.append("<table");
		sbHTMLcache.append(strTableAttribute);
		sbHTMLcache.append(">");
		
		ArrayList<String> alCache;
		this.intRowLength = this.alContent.size();
		for(int i = 0; i < this.intRowLength; i++){
			sbHTMLcache.append("<tr");
			if(hmTrAttr.get(i)!= null)
				sbHTMLcache.append(hmTrAttr.get(i));
			sbHTMLcache.append(">");

			alCache = this.alContent.get(i);
			for(int j = 0; j < this.intColumnLength; j++){
				sbHTMLcache.append("<td");
				sbHTMLcache.append(strTdAttribute);
				
				if(hmtTdRowAttr.get(i)!= null && hmtTdRowAttr.get(i).get(j) != null)
					sbHTMLcache.append(hmtTdRowAttr.get(i).get(j));
				sbHTMLcache.append(">");
				
				if(alCache.get(j) != null)
					sbHTMLcache.append(alCache.get(j));
				else
					sbHTMLcache.append(strDefaultContent);
				
				sbHTMLcache.append("</td>");
			}
			
			sbHTMLcache.append("</tr>\n");
		}
		sbHTMLcache.append("</table>");
		
		return sbHTMLcache.toString();
	}
	
	public void vdClear()
	{
		this.alContent.clear();
	}
	
	public int iGetAutoSize()
	{
		return this.alContent.size();
	}
}